package com.ticket.sass.common.entity.tenant;

import com.baomidou.mybatisplus.annotation.*;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.io.Serializable;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * <p>
 * 会员卡充值项
 * </p>
 *
 * @author ywb
 * @since 2024-03-06
 */
@Data
@EqualsAndHashCode(callSuper = false)
@TableName("member_card_recharges")
@ApiModel(value = "MemberCardRechargesEntity对象", description = "会员卡充值项")
public class MemberCardRechargeEntity implements Serializable {

    @ApiModelProperty(value = "主键")
    @TableId(value = "id", type = IdType.AUTO)
    private Long id;

    @ApiModelProperty(value = "封面图")
    private String cover;

    @ApiModelProperty(value = "充值金额")
    private BigDecimal amount;

    @ApiModelProperty(value = "赠送金额")
    private BigDecimal giftAmount;

    @ApiModelProperty(value = "是否启用")
    private Boolean enable;

    @ApiModelProperty(value = "促销文案")
    private String tips;
    
    @ApiModelProperty(value = "有效开始时间")
    private LocalDate validStartTime;
    
    @ApiModelProperty(value = "有效结束时间")
    private LocalDate validEndTime;

    @ApiModelProperty(value = "创建时间")
    @TableField(fill = FieldFill.INSERT)
    public LocalDateTime createdAt;

    @ApiModelProperty(value = "更新时间")
    @TableField(fill = FieldFill.INSERT_UPDATE)
    public LocalDateTime updatedAt;

    @TableField(select = false)
    @ApiModelProperty(value = "删除时间")
    @TableLogic(value = "null", delval = "now()")
    public LocalDateTime deletedAt;

    // 根据赠送的充值金额计算折扣, amount / amount + giftAmount`
    public BigDecimal calculatedDiscountOnGiftAmount() {
        if (giftAmount == null || giftAmount.compareTo(BigDecimal.ZERO) == 0) {
            return null;
        }
        return amount.divide(amount.add(giftAmount), 4, RoundingMode.HALF_UP)
                .multiply(new BigDecimal(10))
                .setScale(1, RoundingMode.HALF_UP);
    }
}
